package com.kuaimacode.kframework.agent.services.serv;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.category.AgentServMapper;
import com.kuaimacode.kframework.mybatis.models.category.AgentServ;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import com.kuaimacode.kframework.service.DictService;
import com.kuaimacode.kframework.util.DictUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.management.Agent;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/03/22
 */
@Service
public class AgentServService extends ServiceImpl<AgentServMapper, AgentServ> {

	@Autowired
	private DictService dictService;


	/**
	 *  分页查找所有的数据
	 * @param pageNum 当前页码，从1开始
	 * @param pageSize  一页显示多少行
	 * @return
	 */
	public PageInfo<AgentServ> findAll(int pageNum, int pageSize,int type) {
		PageHelper.startPage(pageNum, pageSize);
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("agent_id", SessionUtil.getAgentId());
		queryWrapper.eq("service_class", type);
		queryWrapper.orderByAsc("service_class","service_code");
		List<AgentServ> datas = baseMapper.selectList(queryWrapper);
		return new PageInfo<AgentServ>(datas);
	}

	public List<AgentServ> queryAll(int type) {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("agent_id", SessionUtil.getAgentId());
		queryWrapper.eq("service_class", type);
		queryWrapper.orderByAsc("service_class","service_code");
		List<AgentServ> datas = baseMapper.selectList(queryWrapper);
		return datas;
	}

	@Transactional
	public String saveService(AgentServ agentService){
		String serviceClass = agentService.getServiceClass();
		//校验该二级分类是否已经设置
		boolean isExists = checkIsExists(agentService);
		if (isExists){
			return "保存失败，该二级分类已经设置";
		}
		//保存agentService 表
		agentService.setServiceName(DictUtil.keyValue("SERVICE_CLASS_"+agentService.getServiceClass(),agentService.getServiceCode()));
		agentService.setAgentId(SessionUtil.getAgentId());
		agentService.setCreateAt(new Date());
		agentService.setCreateBy(SessionUtil.getUserId());
		agentService.setDeleteStatus(Constant.DeleteStateEnum.NORMAL.getCode());
		baseMapper.insert(agentService);

		return "success";
	}
	//更新service
	@Transactional
	public String updateService(AgentServ agentService){


		//更新服务表
		agentService.setUpdateAt(new Date());
		agentService.setUpdateBy(SessionUtil.getUserId());
		baseMapper.updateById(agentService);


		return "success";
	}

	public boolean checkIsExists(AgentServ agentServ){
		QueryWrapper queryWrapper = new QueryWrapper();

		queryWrapper.eq("service_class",agentServ.getServiceClass());
		queryWrapper.eq("service_code",agentServ.getServiceCode());
		queryWrapper.eq("agent_id",SessionUtil.getAgentId());
		return baseMapper.selectCount(queryWrapper)>0?true:false;

	}


	/**
	 * 根据分站id 和一级分类删除二级分类
	 * @param agentId
	 * @param serviceClass
	 * @return
	 */
	public int removeByOneClass(String agentId,String serviceClass){
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("agent_id",agentId);
		queryWrapper.eq("service_class",serviceClass);

		return baseMapper.delete(queryWrapper);

	}

	public List<AgentServ> findAgetnServByAgentId(String agentId){
		QueryWrapper<AgentServ> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("agent_id",agentId);
		return baseMapper.selectList(queryWrapper);
	}







}

